#because operator-postfix-increment is the wrong solution for 3 different reasons
Explore tagged Tumblr posts
shieldfoss · 1 year ago
Text
/disagree
There is no interpretation of the problem-as-stated-by-op where "add 1" is the complete solution.
OP says to do this operation on "an integer"
It cannot mean "the platonic model of an integer" because those have no bits and also cannot be represented in digital logic e.g. the integer might be Graham's Number.
It also cannot mean "keyword integer" because C doesn't have that keyword.
it could mean "keyword int." Probably not - OP uses "uint32_t" but it could. In that case, adding 1 to 0xFFFFFFFF is undefined behavior.
it could mean "machine word integer" which is more probable given the use of "uint32_t" as the input. The machine word integer does not have a carry bit.
BUT
PLAYING with the IDEA that you should always increment by 1, the correct implementation does not use c++, it uses ++c:
a = c++; -> No. a does not contain the value as specified.
a = func(c) where func(c){return c++;} -> No. a does not etc./
a = ++c; -> No. a correct, but c modified and spec "a function" not implemented.
a = func(c) where func(c){return ++c;} -> No. Correct implementation of functional requirement, weird implementation of default nonfunctional requirement "perf" (assembly different from "return c+1")
a = func(c) where func(c){return c+1;} -> Correct.
Godbolt
"But that's in debug mode turn on the optimizer" what you think you can get away with writing bad code just because your clients can optimize? What if they're running in debug mode to find your weird off-by-one mistake?
Tumblr media
[Source]
170 notes · View notes